import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../views/Login"
import Console from "../views/Console/Console"
import DeleteUser from "../views/Console/DeleteUser";
import Member from "../views/Console/Member";
import Post from "../views/Console/Post";
import Data from "../views/Console/Data";
import Welcome from "../views/Console/Welcome";
import User from "../views/User/User";
import Main from "../views/User/Main";
import Projects from "../views/User/Projects";
import MyProjects from "../views/User/MyProjects";
import MyMission from "../views/User/MyMission";
import DataTable from "../views/User/DataTable";
import directed from "../components/content/directed";
import following from "../components/content/following";
import created from "../components/content/created";
import deletedMission from "../components/content/deletedMission";
import allProjects from "../components/content/allProjects";
import deletedProjects from "../components/content/deletedProjects";
import projectData from "../components/content/projectData";
import projectDetail from "../views/User/projectDetail";
import overview from "../components/content/overview";
import targetManagement from "../components/content/targetManagement";
import taskManagement from "../components/content/taskManagement";
import payManagement from "../components/content/payManagement";
import Examine from "../views/User/Examine";
import applyExamine from "../components/content/applyExamine";
import MyExamine from "../components/content/MyExamine";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: '登录',
    component: Login
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/console',
    component: Console,
    children:[
      {
        path: '/',
        name: '后台',
        component: Welcome
      },
      {
        path: 'welcome',
        name: '欢迎界面',
        component: Welcome
      },
      {
        path: 'member',
        name: '企业人员',
        component: Member
      },
      {
        path: 'deleteUser',
        name: '已禁用账号',
        component: DeleteUser
      },
      {
        path: 'data',
        name: '数据管理',
        component: Data
      },
      {
        path: 'post',
        name: '职位',
        component: Post
      }
    ]
  },
  {
    path:"/user",
    component: User,
    children: [{
      path: '/',
      name: '用户端',
      component: Main
    },
  {
    path: 'main',
    name: '主页',
    component: Main
  },
  {
    path: 'projects',
    name: '项目',
    component: Projects,
    children: [{
      path: 'myProjects',
      name: '我的项目',
      component: MyProjects,
      children:[{
        path: 'allProjects',
        name: '全部项目',
        component: allProjects
      },
        {
          path: 'deleted',
          name: '项目回收站',
          component: deletedProjects
        }]
    },
    {
      path: 'myMission',
      component: MyMission,
      children: [
        {
          path: '/',
          name: '任务',
          component: directed
        },
        {
        path: 'directed',
        name: '我负责的',
        component : directed,
      },
        {
          path: 'created',
          name: '我创建的',
          component : created,
        },
        {
          path: 'following',
          name: '我参与的',
          component : following,
        },
        {
          path: 'deleted',
          name: '任务回收站',
          component : deletedMission,
        }]
    },
    {
      path: 'dataTable',
      name: '数据报表',
      component: DataTable,
      children: [{
        path: 'project',
        name: '项目数据',
        component: projectData,
      }]
    },
      {
        path: 'projectDetail',
        name: '项目详情',
        component: projectDetail,
        children: [{
          path: 'overview/:id',
          name: '项目概览',
          component: overview,
        },
          {
            path: 'target/:id',
            name: '目标管理',
            component: targetManagement,
          },
          {
            path: 'task/:id',
            name: '任务管理',
            component: taskManagement,
          },
          {
            path: 'pay/:id',
            name: '费用管理',
            component: payManagement,
          }]
      },
    ]
  },
      {
        path: "examine",
        name: "审批",
        component: Examine,
        children: [{
          path:"apply",
          name: "我申请的",
          component:applyExamine
        },
          {
            path:"myExamine",
            name: "我审批的",
            component:MyExamine
          }]
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  const token = window.localStorage.getItem('token');
  const level = window.localStorage.getItem('level');
  if (to.path === '/login' || to.path === '/') {
    if(token){
      next('/user');
    }else{
      next();
    }
  }else if(to.path === '/console' || to.path === '/console/member' || to.path === '/console/welcome'
  ||to.path === '/console/post'||to.path === '/console/deleteUser' || to.path === '/console/data'){
    if(!token) {
      next('/login');
    }else if(!level || level > 1){
      next('/login')
    }else{
      next();
    }
  }else {
    if (!token) {
      next('/login');
    } else {
      next();
    }
  }
});


export default router
